package com.healMyself.dao.impl;

import com.healMyself.dao.UserDao;
import com.healMyself.pojo.User;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {
    QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());

    /**
     * 添加新用户
     * @param user
     * @return
     */
    @Override
    public int registerUser(User user) {
        //定义sql语句
        String sql = "insert into user value(null, ?, ?, ?, ?, ?, ?)";
        Object[] params = { user.getUserName(),user.getAccount(), user.getPassword(), user.getGender(), user.getAge(), user.getIntro()};
        int row = 0;
        try {
            row = queryRunner.update(sql, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return row;
    }

    /**
     * 用户登录， 判断数据库中是否存在
     * @param inputAccount
     * @param inputPassword
     * @return
     */
    @Override
    public User loginUser(String inputAccount, String inputPassword) {
        //定义sql语句
//        String sql = "";
        String sql = "SELECT * FROM user WHERE account=? AND password=?";
        User u = null;
        try {
            u = queryRunner.query(sql, new BeanHandler<>(User.class), inputAccount, inputPassword);
            System.out.println(u);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return u;
    }
}
